var requests = require('../../requests/request.js');
var utils = require('../../utils/util.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    listData:[],       //热门测试列表
    start:0,           //上拉加载  从第几条开始加载
    pageSize:5,        //每次加载的条数
    loadingmore:true   //没有更多
  },
  onLoad: function () {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000,
    })
    var that = this
    requsetHotList.call(that);
  },
  onReachBottom:function(){    //上拉加载
    // console.log('loading...')
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 1500,
    })
    requsetHotList.call(this);
  },
  //跳转开始测试页面
  toStartPage:function(e){
    var id = e.currentTarget.dataset.pageid;
    wx.navigateTo({
      url: '../starttest/starttest?id=' +id
    })
  }
})


//请求热门测试列表数据
function requsetHotList(){
    var that = this;
    var hotListData = this.data.listData;
    var start = this.data.start;
    var pageSize = this.data.pageSize;
    requests.requestHotList(      
      {
        join_num:start,
        limit:pageSize
      },
      (res) => {   //请求成功回调
        // console.log(res)
        setTimeout(function(){
          wx.hideToast()
        },500)
        if(res.state){
          that.setData({
            listData:hotListData.concat(res.list),
            start:res.join_num
          })
        }else{
          console.log('没有更多帖子')
          that.setData({
            loadingmore:false
          })
        }
      },() => {    
        //请求失败
      },() => {    
        //请求完成
      }
    )
}